﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ITAP.ContourBuilder.Components;
using ITAP.ContourBuilder.Components.Dimensions;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddDiameterDimension : SerializedCommand
    {
        public CommandAddDiameterDimension(ContourControl control)
            : base(control)
        {
        }

        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {

                IEnumerable<IContourComponent> allComponents = this.control.ContourContainer.GetAllInnerComponents();
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ContourHole hole = null;
                foreach (IContourComponent component in allComponents)
                    if (component is ContourHole && component.CanSelectByPoint(point))
                        hole = component as ContourHole;


                if (hole == null)
                {
                    this.Cancel();
                    return;
                }

                foreach (IContourComponent component in hole.ConnectedComponents)
                    if (component is DiameterDimension)
                    {
                        this.Cancel();
                        return;
                    }

                DiameterDimension dimension = (DiameterDimension)DimensionFactory.CreateDiameterDimension(hole, GlobalData.DimensionTextFont, DiameterDimensionType.Left);
                hole.GetRootModel().AddInnerComponent(dimension);
                this.control.UpdateModel();
                this.Exit();
                return;
            }
            else if (state.MouseState.MouseMode == MouseMode.Up && state.MouseState.State.Button == MouseButtons.Left)
                this.Cancel();
        }

        public override string ToString()
        {
            return "Создать диаметр отверстия";
        }
    }
}
